//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.svc;

import com.aidong.dao.wills.ContractSignConfirmDao;
import com.aidong.dao.wills.ContractSignDao;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.ContractSignConfirm;
import com.aidong.service.PrintDataService;
import com.aidong.util.FuncUtil;
import com.aidong.web.controller.BaseController;
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"svc/contractSign"})
public class ContractSignController extends BaseController {
    private static String authKey = "12345678";
    @Resource
    PrintDataService printDataService;
    @Resource
    ContractSignDao contractSignDao;
    @Resource
    ContractSignConfirmDao contractSignConfirmDao;

    public ContractSignController() {
    }

    @RequestMapping({"viewContract"})
    public ModelAndView viewContract(@RequestParam("contractCode") String contractCode, @RequestParam("contractType") String contractType, @RequestParam(value = "auth",required = false) String auth) {
        if(!this.chkAuth(auth, new String[]{contractCode, contractType})) {
            return this.showAlert("验证错误");
        } else {
            ContractSign sign = this.contractSignDao.getByContractCode(contractCode, contractType);
            if(sign == null) {
                return this.showAlert("合同号错误");
            } else {
                StringBuilder msg = new StringBuilder();
                HashMap data = this.printDataService.getContractPrintData(sign.getSaleId().intValue(), sign.getContractType(), msg);
                if(data == null) {
                    return this.showAlert(msg.toString());
                } else {
                    ModelAndView mv = new ModelAndView("print/view");
                    mv.addAllObjects(data);
                    mv.addObject("_picPath", this.getSetting("pic.path"));
                    return mv;
                }
            }
        }
    }

    @RequestMapping({"viewHead"})
    public ModelAndView viewHead(@RequestParam("contractCode") String contractCode, @RequestParam("contractType") String contractType, @RequestParam(value = "auth",required = false) String auth, HttpServletResponse response) {
        if(!this.chkAuth(auth, new String[]{contractCode, contractType})) {
            return this.showAlert("验证错误");
        } else {
            ContractSign contractSign = this.contractSignDao.getByContractCode(contractCode, contractType);
            if(contractSign == null) {
                return this.showAlert("合同号错误");
            } else if(contractSign.getStatus().intValue() >= 2 && contractSign.getImageSign() != null) {
                try {
                    response.setContentType("image/jpeg");
                    ServletOutputStream e = response.getOutputStream();
                    e.write(contractSign.getImageHead());
                    e.flush();
                    e.close();
                } catch (Exception var7) {
                    this.log.error(var7);
                }

                return null;
            } else {
                return this.showAlert("合同未签名");
            }
        }
    }

    @RequestMapping({"viewSign"})
    public ModelAndView viewSign(@RequestParam("contractCode") String contractCode, @RequestParam("contractType") String contractType, @RequestParam(value = "auth",required = false) String auth, HttpServletResponse response) {
        if(!this.chkAuth(auth, new String[]{contractCode, contractType})) {
            return this.showAlert("验证错误");
        } else {
            ContractSign contractSign = this.contractSignDao.getByContractCode(contractCode, contractType);
            if(contractSign == null) {
                return this.showAlert("合同号错误");
            } else if(contractSign.getStatus().intValue() >= 2 && contractSign.getImageSign() != null) {
                try {
                    response.setContentType("image/png");
                    ServletOutputStream e = response.getOutputStream();
                    e.write(contractSign.getImageSign());
                    e.flush();
                    e.close();
                } catch (Exception var7) {
                    this.log.error(var7);
                }

                return null;
            } else {
                return this.showAlert("合同未签名");
            }
        }
    }

    @RequestMapping({"viewSignConfirm"})
    public ModelAndView viewSignConfirm(@RequestParam("saleId") int saleId, @RequestParam("contractType") String contractType, @RequestParam(value = "auth",required = false) String auth, HttpServletResponse response) {
        if(!this.chkAuth(auth, new String[]{String.valueOf(saleId), contractType})) {
            return this.showAlert("验证错误");
        } else {
            ContractSignConfirm contractSign = this.contractSignConfirmDao.getBySaleId(saleId, contractType);
            if(contractSign == null) {
                return this.showAlert("合同号错误");
            } else if(contractSign.getImageSign() == null) {
                return this.showAlert("合同未签名");
            } else {
                try {
                    response.setContentType("image/png");
                    ServletOutputStream e = response.getOutputStream();
                    e.write(contractSign.getImageSign());
                    e.flush();
                    e.close();
                } catch (Exception var7) {
                    this.log.error(var7);
                }

                return null;
            }
        }
    }

    private boolean chkAuth(String auth, String... values) {
        if(this.userSession.getLoginUser() != null) {
            return true;
        } else if(auth != null && auth.length() == 16) {
            try {
                String e = auth.substring(0, 6);
                long time = Long.parseLong(e, 16) * 180000L;
                if(time >= System.currentTimeMillis() - 3600000L && time <= System.currentTimeMillis() + 82800000L) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(e);
                    if(values != null && values.length > 0) {
                        String[] arr$ = values;
                        int len$ = values.length;

                        for(int i$ = 0; i$ < len$; ++i$) {
                            String v = arr$[i$];
                            if(v != null && v.length() > 0) {
                                sb.append(v);
                            }
                        }
                    }

                    sb.append(authKey);
                    return auth.substring(6).equalsIgnoreCase(FuncUtil.Md5(sb.toString()).substring(0, 10));
                } else {
                    return false;
                }
            } catch (Exception var11) {
                this.log.error(var11);
                return false;
            }
        } else {
            return false;
        }
    }

    public static String getAuthCode(String... values) {
        String tim = Long.toHexString(System.currentTimeMillis() / 180000L);
        StringBuilder sb = new StringBuilder();
        sb.append(tim);
        if(values != null && values.length > 0) {
            String[] arr$ = values;
            int len$ = values.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String v = arr$[i$];
                if(v != null && v.length() > 0) {
                    sb.append(v);
                }
            }
        }

        sb.append(authKey);
        return tim + FuncUtil.Md5(sb.toString()).substring(0, 10);
    }
}
